package com.tree.thrive.business.excel;

import cn.hutool.core.collection.ListUtil;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.sax.handler.RowHandler;
import com.alibaba.fastjson.JSON;
import com.tree.thrive.TopTreeApplication;
import com.tree.thrive.adapter.http.region.RegionFeign;
import com.tree.thrive.adapter.http.region.response.NextRegionsItem;
import com.tree.thrive.adapter.http.region.response.RegionDTOItem;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.core.io.Resource;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * @Auther: cheng.tang
 * @Date: 2023/8/30
 * @Description: top-tree
 */
@SpringBootTest(classes = TopTreeApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@Slf4j
public class HandleExcel2Csv {

    @Value("classpath:压测用到的skuNos-830.xlsx")
    private Resource resource;

    @Autowired
    private ExcelReaderService excelReaderService;

    @Autowired
    private RegionFeign regionFeign;

    @Test
    public void handleExcel2CsvTest() throws IOException {
        InputStream inputStream = resource.getInputStream();
        List<String> skuNoList = excelReaderService.readAll(inputStream);
        List<String> resultLine = new ArrayList<>();
        List<List<String>> skuNoLines = ListUtil.split(skuNoList, 20);
        for (List<String> skuNos : skuNoLines) {
            resultLine.add(JSON.toJSONString(skuNos));
        }
        File skuQryFile = File.createTempFile("skuList", ".csv");
        IOUtils.writeLines(resultLine, System.lineSeparator(), new FileWriter(skuQryFile));
        log.info(" {} ", skuQryFile.getAbsoluteFile());
    }


    @Test
    public void fill10W() throws IOException {
        InputStream inputStream = resource.getInputStream();
        List<String> skuNoList = excelReaderService.readAll(inputStream);
        List<List<String>> skuParts = ListUtil.split(skuNoList, 300);
        int index = 0;
        List<RegionDTOItem> regionDTOItemList = regionFeign.queryAllRegion();
        List<String> provinceCityLines = new ArrayList<>();
        for (RegionDTOItem regionDTOItem : regionDTOItemList) {
            String provinceName = regionDTOItem.getName();
            List<NextRegionsItem> cityRegionList = regionDTOItem.getNextRegions();
            for (NextRegionsItem nextRegionsItem : cityRegionList) {
                List<String> skuPartList = skuParts.get(index);
                if (CollectionUtils.isEmpty(skuPartList)) {
                    log.info(" 没有可以可用 ");
                    continue;
                }
                for (String skuNo : skuPartList) {
                    provinceCityLines.add(String.join(",", provinceName, nextRegionsItem.getName(), skuNo));
                }
            }
        }
        File tempFile = File.createTempFile("region", ".csv");
        FileWriter fileWriter = new FileWriter(tempFile);
        IOUtils.writeLines(provinceCityLines, System.lineSeparator(), fileWriter);
        log.info(" {} ", tempFile.getAbsoluteFile());
    }


}
